home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / science / cdrift3.zip / AMI.C next >
C/C++ Source or Header  |  1990-02-22  |  6KB  |  171 lines

  1. /* This program is Copyright (c) 1990 David Allen.  It may be freely
  2.    distributed as long as you leave my name and copyright notice on it.
  3.    I'd really like your comments and feedback; send e-mail to
  4.    davea@vlsi.ll.mit.edu, or send us-mail to David Allen, 10 O'Moore Ave,
  5.    Maynard, MA 01754. */
  6.  
  7.  
  8. /* This file contains only functions which are specific to the Amiga.  It
  9.    connects to the other source files via a small number of functions.
  10.    Main() is here.  It calls init() and onestep().  The other source files
  11.    can call rnd(), draw() and panic() from this file; these are at the end
  12.    of the file.  My purpose here is to make tec?.c machine
  13.    independent, with all the machine dependencies in this file. */
  14.  
  15.  
  16. #include "const.h"
  17. #include "var.h"
  18. #include "exec/types.h"
  19. #include "intuition/intuition.h"
  20. #include "intuition/intuitionbase.h"
  21.  
  22. extern short step; /* Defined in tec1.c */
  23. extern unsigned char t[2][MAXX][MAXY]; /* Defined in tec1.c */
  24. extern unsigned long RangeSeed; /* Amiga random number seed */
  25. extern struct IntuitionBase *IntuitionBase;
  26.  
  27. struct GfxBase        *GfxBase;
  28. struct RastPort       *rp;
  29. struct ViewPort       *vp;
  30. struct Window         *w = NULL;
  31. struct Screen         *screen;
  32. struct IntuiMessage   *message;
  33. unsigned long         class;
  34. unsigned short        code;
  35.  
  36. /* 32 colors - 0-3 are used by Intuition; black background, grey gadgets, */
  37. /* white shadows, red highlighting.  Then there are 5 shades of blue from */
  38. /* dark to light, 5 of green, 5 of red, 5 of purple, 5 white, and 3 yellow. */
  39. /* A 5-bitplane screen is required. */
  40. unsigned short colors [32] = {
  41.    0x000, 0x888, 0xfff, 0x00f,
  42.    0x007, 0x009, 0x00b, 0x00d, 0x00f,
  43.    0x070, 0x090, 0x0b0, 0x0d0, 0x0f0,
  44.    0x700, 0x900, 0xb00, 0xd00, 0xf00,
  45.    0x707, 0x909, 0xb0b, 0xd0d, 0xf0f,
  46.    0x777, 0x999, 0xbbb, 0xddd, 0xfff,
  47.    0x099, 0x0bb, 0x0dd };
  48.  
  49. /* Structures for a custom lores screen of 5 bitplanes with a 320x200 */
  50. /* window; use left-amiga-n and left-amiga-m to get back to workbench screen */
  51.  
  52. struct NewScreen ns = { 0L, 0L, 320L, 200L, 5L, 0, 1,
  53.    NULL, CUSTOMSCREEN, NULL, (UBYTE *)0, NULL, NULL };
  54.  
  55. struct NewWindow nw = {
  56.    0, 0, 320, 200, 0, 1, 0, 0, NULL, NULL,
  57.    (UBYTE *) NULL, NULL, NULL,
  58.    0, 0, 320, 200, CUSTOMSCREEN };
  59.  
  60.  
  61. main (argc, argv) int argc; char **argv; {
  62.    unsigned long t[3];
  63.  
  64.    /* Initialize random number generator using milliseconds of datestamp */
  65.    DateStamp (t);  RangeSeed = t[2];
  66.  
  67.    /* Initialize everything */
  68.    grafinit ();
  69.    init (*++argv);
  70.  
  71.    /* Call onestep () once per step; check for mouse clicks */
  72.    for (step=0; step<MAXSTEP; step++) {
  73.       checkmouse ();
  74.       onestep (); }
  75.  
  76.    /* Loop forever until user clicks close gadget - busy waiting, sorry! */
  77.    while (1 == 1) checkmouse (); }
  78.  
  79.  
  80. grafinit () {
  81.    /* This is a standard kind of setup function; open all the libraries, then
  82.    open the screen, then open the window.  If any of the calls fail, free the
  83.    memory and return 0.  The rastport variable is kept for the drawing
  84.    function and the colors are loaded. */
  85.  
  86.    GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0L);
  87.    IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0L);
  88.    screen = (struct Screen *) OpenScreen (&ns);
  89.    if (screen == NULL) goto c2;
  90.    vp = &screen->ViewPort; nw.Screen = screen;
  91.    nw.Title = "Tectonics";
  92.    nw.Flags = SIMPLE_REFRESH | WINDOWCLOSE;
  93.    nw.IDCMPFlags = MOUSEBUTTONS | CLOSEWINDOW;
  94.    w = (struct Window *) OpenWindow (&nw);
  95.    if (w == NULL) goto c1;
  96.  
  97.    rp = w->RPort; SetDrMd (rp, 0);
  98.    LoadRGB4 (vp, colors, 32);
  99.    return (1);
  100.  
  101.    c1: if (w) CloseWindow (w);
  102.    c2: CloseScreen (screen);
  103.    CloseLibrary (IntuitionBase);
  104.    CloseLibrary (GfxBase);
  105.    return (0); }
  106.  
  107.  
  108. grafexit () {
  109.    /* Just close all the things that were opened, then exit(). */
  110.    CloseWindow (w);
  111.    CloseScreen (screen);
  112.    CloseLibrary (IntuitionBase);
  113.    CloseLibrary (GfxBase);
  114.    exit (0); }
  115.  
  116.  
  117. checkmouse () {
  118.    /* Standard event handler; loop through all the messages that have come in,
  119.    figure out whether they are clicks or gadget hits, and reply to them.
  120.    Outside the loop, act on the clicks.  If the CLOSEWINDOW gadget was hit,
  121.    exit the program; if the left button was clicked anywhere, print out the
  122.    current values of t[src] using tecst () in tec1.c. */
  123.  
  124.    short mousecode = 0, i, j, k;
  125.  
  126.    while ((message=(struct IntuiMessage *)GetMsg(w->UserPort))!=NULL) {
  127.       class = message->Class; code = message->Code;
  128.       ReplyMsg (message);
  129.       if (code == SELECTDOWN) mousecode = 1;
  130.       if (class == CLOSEWINDOW) mousecode = -1; }
  131.    if (mousecode == -1) grafexit ();
  132.    if (mousecode == 1) tecst (step % 2, DRAWMODE); }
  133.  
  134.  
  135. rnd (top) short top; { return (RangeRand (top)); }
  136.    /* RangeRand is located in Amiga.lib */
  137.  
  138.  
  139. panic (s) char *s; { printf ("PANIC: %s\n", s); grafexit (0); }
  140.    /* Used when some fatal inconsistency is found in the database;
  141.    its function is to immediately free all graphics memory and exit. */
  142.  
  143.  
  144. draw (src) short src; {
  145.    /* Takes the array m[src] and draws it on the screen, in a hopefully
  146.    efficient way.  The function tries to make long horizontal
  147.    patches that are all the same color.  Then they can be rendered by a
  148.    graphics function that draws arbitrary rectangles quickly. */
  149.  
  150.    register short i, j, k, x;
  151.  
  152.    /* Erase the screen first by drawing a big black box */
  153.    SetAPen (rp, 0); RectFill (rp, 14, 10, 280, 189); 
  154.  
  155.    /* For each scan line, start at the left edge */
  156.    for (j=0, i=0; j<YSIZE; j++, i=0) {
  157.  
  158.       /* If the current square is not ocean, set x to its color */
  159.       top: if ((x = t[src][i][j] >> 2) > 1) {
  160.  
  161.          /* Go as far along to the right as you can in this color */
  162.          k = i+1; while (((t[src][k][j] >> 2) == x) && (k < XSIZE-1)) k++;
  163.  
  164.          /* Draw a short, wide rectangle */
  165.          if (x > 27) x = 27; SetAPen (rp, x+4);
  166.          RectFill (rp, 3*i + 14, 2*j + 10, 3*k + 13, 2*j + 11);
  167.          i = k-1; }
  168.  
  169.       /* If not at end of scanline, do more; else start next scanline */
  170.       if (i < XSIZE-1) { i++; goto top; } } }
  171.